package com.jdkj.tools.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jdkj.tools.domain.ProjectDetail;
import com.jdkj.tools.domain.ProjectType;
import com.jdkj.tools.util.*;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {

    @GetMapping()
    public AjaxResult getProject() {
        List<ProjectType> list = ParseProject.parseHtml();
        return AjaxResult.success(list);
    }

    @GetMapping("/list")
    public AjaxResult getProjectDetail(@RequestParam("projectKey") String projectKey) {
        System.out.println("projectKey"+projectKey);
        StringBuffer str=new StringBuffer("http://192.168.11.7:8083/rest/raven/1.0/folderStructure/folders?entityKey=").append(projectKey).append("&folderId=4").append("&_=").append(System.currentTimeMillis());
        Header [] header=new Header[]{new BasicHeader("Cookie", TokenConstant.JSESSIONID)};
        String res= HttpClientUtil.doGet(str.toString(), new HashMap<>(), header);
        System.out.println("详细结果"+res);
        JSONObject jsonObject = JSONObject.parseObject(res);
        List<ProjectDetail> list=new ArrayList<>();
        JSONArray folders = jsonObject.getJSONArray("folders");
        System.out.println("数组长度"+folders.size());
        for (int i = 0; i < folders.size(); i++) {
            JSONObject folder = folders.getJSONObject(i);
            ProjectDetail detail = new ProjectDetail();
            detail.setName(folder.getString("name"));
            detail.setId(folder.getInteger("id"));
            list.add(detail);
        }
        return AjaxResult.success(list);
    }
}
